/* 
 * File:   Base_SearchEnginer.cpp
 * Author: vicror
 * 
 * Created on October 24, 2011, 1:03 AM
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "Base_SearchEnginer.h"
#include "Vocabluary.h"
#include <cstddef>
namespace SearchEnginer {
    #define null 0;
    Base_SearchEnginer::Base_SearchEnginer() {
        vocabluary = null;
        vocabluary = Vocabluary::Init(*this);

    }

    Base_SearchEnginer::Base_SearchEnginer(const Base_SearchEnginer& orig) {
        vocabluary = null;
        link(orig);
    }

    Base_SearchEnginer::~Base_SearchEnginer() {
        if (vocabluary != 0) vocabluary->unlink(*this);
    }

    void Base_SearchEnginer::unLink() {
        if (vocabluary != 0) vocabluary->unlink(*this);
        vocabluary = Vocabluary::Init(*this);
    }

    void Base_SearchEnginer::link(const Base_SearchEnginer& orig) {
        if (orig.vocabluary != 0) {
            orig.vocabluary->link(*this);
        } else {
            unLink();
        }
    }

    void Base_SearchEnginer::syncUnLink() {
        vocabluary = null;
        OnClear();
    }


    void Base_SearchEnginer::syncVocabluary(Vocabluary *orig) {
        if(vocabluary != 0) vocabluary->unlink(*this);
        vocabluary = orig;
        OnSync();
    }


    bool Base_SearchEnginer::append(const string& orig) {
        if (vocabluary != 0) {
            return vocabluary->append(orig);
        } else {
            return false;
        }
    }

    bool Base_SearchEnginer::append(const Base_SearchEnginer& orig) {
        if (vocabluary != 0 && orig.vocabluary != 0) {
            return vocabluary->append(*orig.vocabluary);
        } else {
            return false;
        }
    }

    ListIndex Base_SearchEnginer::hasWordByID(const string& /*orig*/) const {
        ListIndex res;
        return res;
    }

    ListString Base_SearchEnginer::hasWord(const string& orig) const {
        return vocabluary->getWords(hasWordByID(orig));
    }

    void Base_SearchEnginer::OnClear() {

    }

    void Base_SearchEnginer::OnSync() {

    }

    void Base_SearchEnginer::syncWord(const string& /*orig*/, Index /*index*/) {

    }

    std::ostream& operator<< (std::ostream& out, const Base_SearchEnginer &orig) {
        if (orig.vocabluary != 0) out << *orig.vocabluary;
        return out;
    }

    std::istream& operator>> (std::istream& is, Base_SearchEnginer& orig) {
        if (orig.vocabluary != 0) is >> *orig.vocabluary;
        return is;

    }



}